#ifndef CVECTOR3_H
#define CVECTOR3_H

/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool.

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.

*/

#include <iostream>
#include <math.h>

//3D vector. simplifies most of the operations in the physics and graphics systems
class CVector3
{
public:

    CVector3(); //empty constructor
    CVector3(float x, float y, float z); //constructor. vector components are initialised with x, y, z
    CVector3(float components[3]); //constructor. takes a pointer to float. nice to have when initialising
    //vectors from rows of CMatrix4x4
    CVector3(const CVector3 &source); //copy constructor
    ~CVector3(); //destructor

    float& operator() (const unsigned short i);//() overload. outputs mData[i]. i must be between 0 and 2
    CVector3 operator+ (CVector3 vector); //+ overload, sum of 2 vectors
    CVector3 operator+ (const float scalar); //+ overload, sum of vector and scalar constant
    CVector3 operator- (CVector3 vector); //- overload, subtraction of 2 vectors
    CVector3 operator- (const float scalar); //- overload, subtraction of scalar constant
    CVector3 operator* (CVector3 vector); //* overload: element-wise multiplication
    CVector3 operator* (const float scalar); //* overload: multiplication by a scalar value
    CVector3 operator/ (CVector3 vector); //* overload: element-wise division
    CVector3 operator/ (const float scalar); //* overload: division by a scalar value
    float Dot(CVector3 vector); //scalar (dot) product.
    CVector3 Cross(CVector3 vector); //vector (cross) product
    CVector3& operator= (CVector3 vector); //= overload
    bool operator== (CVector3 vector);//== overload
    bool operator!= (CVector3 vector); //!= operator overload

    inline float GetLength();
    inline CVector3 Normalize();
    inline void SetComponents(float x, float y, float z); //sets (or re-sets) the vector components to x, y, z
    inline float* GetComponents(); //provides in output the pointer to mData
    inline float x(); //returns mData[0]
    inline float y(); //returns mData[1]
    inline float z(); //returns mData[2]

    static CVector3 RotateX(float angle, CVector3& in);
    static CVector3 RotateY(float angle, CVector3& in);
    static CVector3 RotateZ(float angle, CVector3& in);

    // non-member operators
    friend std::ostream & operator << (std::ostream &out, CVector3 &vec);

private:

    float mData[3];
};

inline float CVector3::GetLength()
{
    return sqrt( mData[0]*mData[0] + mData[1]*mData[1] + mData[2]*mData[2] );
}

inline CVector3 CVector3::Normalize()
{
	CVector3 out;

	float mag = GetLength();

	out.mData[0] = mData[0] / mag;
	out.mData[1] = mData[1] / mag;
	out.mData[2] = mData[2] / mag;

	return out;
}

inline void CVector3::SetComponents(float x, float y, float z)
{
    mData[0] = x;
    mData[1] = y;
    mData[2] = z;
}

inline float* CVector3::GetComponents()
{
    return mData;
}

inline float CVector3::x()
{
    return mData[0];
}

inline float  CVector3::y()
{
    return mData[1];
}

inline float  CVector3::z()
{
    return mData[2];
}

#endif // CVECTOR3_H
